import { Group, Vector3 } from "three";
import { SHAPE_TYPES } from "cannon-es";

const defaultTrees = [
  new Vector3(18.4949, -21.9102, 0),
  new Vector3(19.6496, -21.9102, 0),
  new Vector3(4.15384, -24.487, 0),
  new Vector3(21.1227, -21.0812, 0),
  new Vector3(22.1645, -23.562, 0),
  new Vector3(25.2483, -8.6961, 0),
  new Vector3(28.142, -6.64796, 0),
  new Vector3(23.5403, -5.8931, 0),
  new Vector3(21.8172, -3.89102, 0),
  new Vector3(19.2888, -3.20587, 0),
  new Vector3(21.5826, -2.62404, 0)
];

export default class Trees {
  constructor(physics) {
    this.physics = physics;
    this.models = [];
    this.main = new Group();
  }

  createTree(position) {
    const index = Math.round(Math.random());
    const scale = Math.random() * .8 + .8;
    const tree = this.models[index].clone();
    tree.position.copy(position);
    tree.scale.set(scale, scale, scale);
    const body = this.physics.createBody({
      mesh: tree,
      shapeType: SHAPE_TYPES.CYLINDER,
      mass: 0
    });
    body.sleep();
    // const shadow = new CustomShadow(CustomShadowType.circle)
    // shadow.build(tree)
    // this.main.add(shadow.main)
    return tree;
  }

  add(modelMesh) {
    this.models.push(modelMesh);
  }

  build() {
    defaultTrees.forEach(position => {
      const tree = this.createTree(position);
      this.main.add(tree);
    });

    const min = 25;
    const max = 80;
    const nums = 20;
    for (let i = 0; i < nums; i++) {
      const distance = Math.random() * (max - min) + min;
      const angle = Math.random() * Math.PI * 2;
      const position = new Vector3(
        Math.cos(angle) * distance,
        Math.sin(angle) * distance,
        0
      );
      const tree = this.createTree(position);
      this.main.add(tree);
    }

    this.models.forEach(e => {
      e.parent?.remove(e);
    });
  }
}